package org.crystalcore;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.ItemStack;
import static org.crystalcore.CrystalCore.total_detection;

public class DamageLimiter implements Listener {
    @EventHandler
    public void onInventoryOpen(InventoryOpenEvent event) {
        if (!(event.getPlayer() instanceof Player)) {
            return;
        }

        Player player = (Player) event.getPlayer();
        ItemStack[] contents = player.getInventory().getContents();

        for (ItemStack item : contents) {
            if (item != null && item.getType() != Material.AIR) {
                if (item.containsEnchantment(Enchantment.DAMAGE_ALL)) {
                    int enchantmentLevel = item.getEnchantmentLevel(Enchantment.DAMAGE_ALL);
                    if (enchantmentLevel == 32767) {
                        player.getInventory().remove(item);
                    }
                }
            }
        }
    }
//    @EventHandler
//    public void onPlayerInteract(PlayerInteractEvent event){
//
//            Player player = (Player) event.getPlayer();
//            ItemStack[] contents = player.getInventory().getContents();
//
//            for (ItemStack item : contents) {
//                if (item != null && item.getType() != Material.AIR) {
//                    if (item.containsEnchantment(Enchantment.DAMAGE_ALL)) {
//                        int enchantmentLevel = item.getEnchantmentLevel(Enchantment.DAMAGE_ALL);
//                        if (enchantmentLevel == 32767) {
//                            player.getInventory().remove(item);
//                        }
//                    }
//                }
//            }
//        }
//
//    @EventHandler
//    public void onInventoryClick(InventoryClickEvent event){
//
//            ItemStack[] contents = event.getInventory().getContents();
//
//            for (ItemStack item : contents) {
//                if (item != null && item.getType() != Material.AIR) {
//                    if (item.containsEnchantment(Enchantment.DAMAGE_ALL)) {
//                        int enchantmentLevel = item.getEnchantmentLevel(Enchantment.DAMAGE_ALL);
//                        if (enchantmentLevel == 32767) {
//                            event.getInventory().remove(item);
//                        }
//                    }
//                }
//            }
//        }

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        Player player = (Player) event.getDamager();
        if (event.getDamage() > 20) {
            if (total_detection) {
                event.setCancelled(true);
            }
            player.getInventory().setItemInMainHand(null);
            if (player.getInventory().getItemInOffHand().getType() == Material.DIAMOND_SWORD) {
                player.getInventory().setItemInOffHand(null);
            }
        }
    }
}
